package com.netease.xone.fragment;

import java.util.List;

import protocol.XoneCallBack;
import protocol.XoneService;
import protocol.meta.RecommAppInfo;
import Utils.XoneUtil;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.netease.framework.widget.LinkedListAdapter;
import com.netease.xone.R;
import com.netease.xone.widget.LoadingImageView;


public class FragMoreApp extends FragmentBase {
    private ListView mList;
    private MoreAppAdapter mAdater;
    private boolean mBSmallIcon;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        getActivity().setTitle(R.string.setting_item_recommond);
        View view = inflater.inflate(R.layout.fragment_more_app, container, false);
        findViews(view);
        return view;
    }
    private void findViews(View view){
        mList = (ListView) view.findViewById(R.id.more_app_list);
        mAdater = new MoreAppAdapter(getActivity());
        mList.setAdapter(mAdater);
        mList.setOnItemClickListener(mItemClick);
    }
    
    OnItemClickListener mItemClick = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            RecommAppInfo info = mAdater.getItem(position);
            if (!XoneUtil.startOtherApp(getActivity(), info.getSchema())) {
                try {
                    Uri uri = Uri.parse(info.getDownloadUrl());
                    Intent it = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(it);
                } catch (Exception e) {
                    e.printStackTrace();                    
                }
            }
        }
    };
    
    class MoreAppAdapter extends LinkedListAdapter<RecommAppInfo> {
        private LayoutInflater mInflater;

        public MoreAppAdapter(Context context) {
            mInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                View v = mInflater.inflate(R.layout.item_more_app,null);
                ViewHolder holder = new ViewHolder();
                holder.appName = (TextView) v.findViewById(R.id.reco_app_name);
                holder.appDesc = (TextView) v
                        .findViewById(R.id.reco_app_describe);
                holder.appIcon = (LoadingImageView) v
                        .findViewById(R.id.reco_app_icon);
//                holder.appIcon.setLoadingIcon(R.color.trans);
                holder.goButton = (TextView) v
                        .findViewById(R.id.reco_app_button);
                v.setTag(holder);
                convertView = v;
            }
            ViewHolder holder = (ViewHolder) convertView.getTag();
            RecommAppInfo info = getItem(position);
            holder.appName.setText(info.getDisplayName());
            holder.appDesc.setText(info.getDescription());
            if(mBSmallIcon){
                holder.appIcon.setLoadingImage(info.getlIconUrl());
            } else {
                holder.appIcon.setLoadingImage(info.gethIconUrl());
            }
            if (XoneUtil.hasApp(getActivity(), info.getSchema())) {
                holder.goButton.setText(R.string.more_app_open);
            } else {
                holder.goButton.setText(R.string.more_app_download);
            }
            return convertView;
        }

    }

    class ViewHolder {
        TextView appName;
        TextView appDesc;
        LoadingImageView appIcon;
        TextView goButton;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        XoneService.getInstance().addListener(mXoneCallBack);
        XoneService.getInstance().doGetRecommApp();
        DisplayMetrics dm = new DisplayMetrics();  
        dm = getResources().getDisplayMetrics();  
        float density  = dm.density;        //
        Log.i("about", "dm.density is " + dm.density);
        if (density < 2.0) {
            mBSmallIcon = true;
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        XoneService.getInstance().removeListener(mXoneCallBack);
    };
    XoneCallBack mXoneCallBack = new XoneCallBack() {
        @Override
        public void onGetRecomm(int transactionId,List<RecommAppInfo> list) {
            if(mAdater != null){
                mAdater.clear();
                mAdater.addTail(list);
            }
        };

        @Override
        public void onGetRecommError(int transactionId, int errCode, String err) {
              showToast(err);
        };
    };
}
